package com.linnas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linnas.exception.CustomException;
import com.linnas.pojo.Category;
import com.linnas.pojo.Pet;
import com.linnas.service.CategoryService;
import com.linnas.mapper.CategoryMapper;
import com.linnas.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 亦怀
* @description 针对表【category(宠物分类)】的数据库操作Service实现
* @createDate 2022-12-25 15:17:14
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private PetService petService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, Pet::getCategoryId, id);
        long count = petService.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("当前分类已关联宠物，不能删除");
        }
        super.removeById(id);

    }
}




